package plugins.detail;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.AfterAddRowEventArgs;
import kd.bos.entity.datamodel.events.AfterDeleteRowEventArgs;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.sdk.plugin.Plugin;

import java.math.BigDecimal;
import java.util.EventObject;

/**
 * 单据界面插件
 */
public class CalculateValue extends AbstractBillPlugIn implements Plugin {

    // 单据体标识
    private static final String ENTRY_ENTITY_KEY = "dcb4_purchased_dishes";
    // 单据体金额字段标识
    private static final String SUM_FIELD_KEY = "dcb4_sum";
    // 单据头总金额字段标识
    private static final String TOTAL_AMOUNT_FIELD_KEY = "dcb4_amount_sum";

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        // 新建单据时，初始化总金额为0
        calculateTotalSum();
    }

    @Override
    public void afterLoadData(EventObject e) {
        super.afterLoadData(e);
        // 数据加载完成后立即计算一次总和
        calculateTotalSum();
    }

    // 处理单据体字段值改变事件
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        // 只处理我们关心的字段
        if (SUM_FIELD_KEY.equals(e.getProperty().getName())) {
            calculateTotalSum();
        }
    }

    // 处理单据体行增加事件
    @Override
    public void afterAddRow(AfterAddRowEventArgs e) {
        calculateTotalSum();
    }

    // 处理单据体行删除事件
    @Override
    public void afterDeleteRow(AfterDeleteRowEventArgs e) {
        calculateTotalSum();
    }

    // 计算单据体所有行中dcb_sum字段的总和
    private void calculateTotalSum() {
        try {
            // 获取单据体所有行
            DynamicObjectCollection entryRows = this.getModel().getEntryEntity(ENTRY_ENTITY_KEY);
            BigDecimal total = BigDecimal.ZERO;

            // 遍历所有行，累加dcb_sum字段的值
            for (DynamicObject row : entryRows) {
                Object value = row.get(SUM_FIELD_KEY);

                // 安全处理各种数字类型
                if (value instanceof BigDecimal) {
                    total = total.add((BigDecimal) value);
                } else if (value instanceof Integer) {
                    total = total.add(BigDecimal.valueOf((Integer) value));
                } else if (value instanceof Double) {
                    total = total.add(BigDecimal.valueOf((Double) value));
                } else if (value instanceof Long) {
                    total = total.add(BigDecimal.valueOf((Long) value));
                } else if (value instanceof String) {
                    try {
                        total = total.add(new BigDecimal((String) value));
                    } catch (NumberFormatException ex) {
                        // 忽略格式错误的字符串
                    }
                }
            }

            // 将总和设置到单据头字段
            this.getModel().setValue(TOTAL_AMOUNT_FIELD_KEY, total);

            // 更新界面显示
            this.getView().updateView(TOTAL_AMOUNT_FIELD_KEY);

        } catch (Exception e) {
            this.getView().showErrorNotification("计算金额总和时出错: " + e.getMessage());
        }
    }
}